package com.micro.ai.commons.domain;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * 分页请求模型
 * 
 * @author micro-ai
 * @since 0.0.1
 */
public record PageRequest(
    /**
     * 当前页码，从1开始
     */
    @NotNull(message = "页码不能为空")
    @Min(value = 1, message = "页码不能小于1")
    Integer current,
    
    /**
     * 页大小
     */
    @NotNull(message = "页大小不能为空")
    @Min(value = 1, message = "页大小不能小于1")
    @Max(value = 1000, message = "页大小不能超过1000")
    Integer size,
    
    /**
     * 排序字段
     */
    String orderBy,
    
    /**
     * 排序方向
     */
    String orderDirection
) implements Serializable {

    /**
     * 默认构造函数
     */
    public PageRequest {
        if (current == null) {
            current = 1;
        }
        if (size == null) {
            size = 10;
        }
        if (orderDirection == null) {
            orderDirection = "ASC";
        }
    }

    /**
     * 获取偏移量
     */
    public long getOffset() {
        return (long) (current - 1) * size;
    }

    /**
     * 获取限制数量
     */
    public int getLimit() {
        return size;
    }

    /**
     * 判断是否需要排序
     */
    public boolean hasOrderBy() {
        return orderBy != null && !orderBy.trim().isEmpty();
    }

    /**
     * 判断是否为升序
     */
    public boolean isAsc() {
        return "ASC".equalsIgnoreCase(orderDirection);
    }

    /**
     * 判断是否为降序
     */
    public boolean isDesc() {
        return "DESC".equalsIgnoreCase(orderDirection);
    }
}
